package jmine.tec.di.validation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import jmine.tec.di.metadata.DependencyMetaData;
import jmine.tec.di.scope.TypedScope;
import jmine.tec.di.type.TypeConvertionException;
import bancosys.tec.persist.Persistable;
import bancosys.tec.persist.base.enumx.CouldNotGetEnumException;

/**
 * 
 */
public final class StaticValidator {

    /**
     * Construtor.
     */
    private StaticValidator() {
        super();
    }

    /**
     * @param props {@link TypedScope}
     * @param meta {@link DependencyMetaData}
     * @return {@link List}
     */
    public static List<String> validateSimple(TypedScope props, DependencyMetaData... meta) {
        List<String> list = new ArrayList<String>();
        for (DependencyMetaData dependencyMetaData : meta) {
            if (dependencyMetaData.isRequired() && !dependencyMetaData.isResolvable(props)) {
                list.add("O campo \"" + dependencyMetaData.getPropertyName() + "\" é obrigatório.");
            }
            try {
                if (Persistable.class.isAssignableFrom(dependencyMetaData.getComponentType())) {
                    continue;
                }
                dependencyMetaData.resolveObjectWrap(props);
            } catch (TypeConvertionException e) {
                list.add(e.getMessage());
            } catch (CouldNotGetEnumException e) {
                Object value = props.get(dependencyMetaData.getPropertyName(), Object.class);
                list.add(dependencyMetaData.getPropertyName() + " \"" + value + "\" não encontrado(a).");
            }
        }
        return list;
    }

    /**
     * @param props {@link TypedScope}
     * @param dependencies {@link Collection} of {@link DependencyMetaData}
     * @return {@link List}
     */
    public static List<String> validateSimple(TypedScope props, Collection<? extends DependencyMetaData> dependencies) {
        return validateSimple(props, dependencies.toArray(new DependencyMetaData[dependencies.size()]));
    }

}
